home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pascal / 40int24.zip / WP.DOC < prev   
Text File  |  1988-01-02  |  5KB  |  121 lines

  1. (* WP  --  A demonstration unit to disable trapping of write protect errors.
  2.  
  3.   This file includes a sample custom critical error handler for the INT24 unit
  4. for Turbo Pascal version 4.0.  The INT24 unit allows installation of special
  5. handlers to be installed before the default handler is called and deal with
  6. special cases.  In this case, we decided not to allow retries after attempting
  7. to write to a disk with a write-protect tab.
  8.   A skeleton critical error handler must look like this:
  9.  
  10.         var SaveCriticalProc: pointer;
  11.  
  12.         {$F+}
  13.         procedure CriticalErrorHandler(var Retry: boolean;
  14.                                            ErrorCode: word;
  15.                                        var DeviceName: string);
  16.           begin
  17.           if ErrorCode = { Whatever errors we are interested in }
  18.            then
  19.             { Handle the error }
  20.            else
  21.             { Pass on any unwanted errors to other critical error handlers }
  22.             CriticalProc := SaveCriticalProc
  23.           end;
  24.         {$F-}
  25.  
  26.         begin
  27.         SaveCriticalProc := CriticalProc; { This installs your handler }
  28.         CriticalProc := @CriticalErrorHandler;
  29.         { Rest of your unit or program }
  30.         end.
  31.  
  32.   The compiler directives ensure that the handler routine is compiled as a FAR
  33. procedure and this is _absolutely_ necessary.  Note that there is no error
  34. checking that the procedure header matches this example, but if it doesn't the
  35. program will die when a critical error does occur.  The Retry flag is set true
  36. in your handler if you decide you want to retry the operation.  To abort the
  37. operation, leave Retry false and simply end the procedure.  To pass on control
  38. to the next error handler, leave Retry false and restore the value of
  39. CriticalProc.  The ErrorCode parameter will be a equal to the value return by
  40. DOS for the critical error and ranges from 0 to 12 in the same order as those
  41. errors from Turbo 4 (i.e. 0 corresponds to 150 and is write-protect error).
  42. DeviceName is a string variable that will either contain the drive the error
  43. occured on (such as 'A:'), the character device the error occured on (such as
  44. 'PRN') or be empty ('') in the case of a bad FAT.
  45.   In general the flow of logic in a critical error handler is to first decide
  46. if this is an error type the handler is interested in by checking ErrorCode.
  47. If not, then restore the address that was in CriticalProc before you installed
  48. this handler.  This permits a series of independant critical error handlers to
  49. be installed at the same time.  If your handler deals with an error, then
  50. leave CriticalProc alone and any other handlers will be disabled for this
  51. critical error.  Assuming that this is an error you want to deal with, do
  52. whatever you want to do (display a message, correct the problem, or whatever)
  53. and either leave Retry alone (which will cause the function to fail and
  54. your code deals with the error) or set Retry to true and let DOS try the
  55. operation again.  Note that you are very limited as to what you can do in a
  56. critical error handler.  Generally, you may not use any DOS calls but rather
  57. must either interface with the hardware directly or use BIOS calls.  The
  58. default handler uses BIOS calls and direct access to the display through the
  59. FastWr unit.  This is a good choice.
  60.   To install a critical error handler, save the current contents of the
  61. CriticalProc variable and then set CriticalProc to the address of your
  62. critical error handler.  No facility for uninstalling critical error handlers
  63. is provided.
  64.   Compile this file and compare its operation to the test in INT24.DOC.  The
  65. only difference should be an immeadiate error result when writing to a disk
  66. with a write protect tab. *)
  67.  
  68.  
  69. program Test;
  70.  
  71. uses Int24;
  72.  
  73. (******************* This is the special handler *******************)
  74. var SaveCriticalProc: pointer;
  75.  
  76. {$F+}
  77. procedure CriticalErrorHandler(var Retry: boolean;
  78.                                    ErrorCode: word;
  79.                                var DeviceName: string);
  80.   { Automatically abort on write-protect errors }
  81.   begin
  82.   if ErrorCode = 0                     { Watch for write protect error }
  83.    then
  84.     { Do absolutly nothing to abort automatically }
  85.    else
  86.     CriticalProc := SaveCriticalProc   { Pass error on to default handler }
  87.    end;
  88. {$F-}
  89. (*******************)
  90.  
  91. {$I-}
  92.  
  93. var I: Integer;
  94.  
  95. procedure FileTest;
  96.   var F: file;
  97.   begin
  98.   writeln('Testing for critical errors by writing to drive A:');
  99.   I := IOResult;
  100.   assign(F,'A:FILE');
  101.   I := IOResult;
  102.   rewrite(F);
  103.   I := IOResult;
  104.   if I <> 0
  105.    then
  106.     writeLn('Create failure on A:FILE :  IOResult=',I)
  107.    else
  108.     begin
  109.     writeln('A:FILE created.');
  110.     I := IOResult;
  111.     close(F);
  112.     I := IOResult
  113.     end
  114.   end;
  115.  
  116. begin
  117. SaveCriticalProc := CriticalProc;      { This installs the special handler }
  118. CriticalProc := @CriticalErrorHandler;
  119. FileTest
  120. end.
  121.